package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.domain.SystemDictionary;
import cn.wolfcode.ssm.domain.SystemDictionaryItem;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.ssm.service.ISystemDictionaryItemService;
import cn.wolfcode.ssm.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

   @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")SystemDictionaryItemQueryObject qo) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.selctForPageResult(qo);
        //查询出所有的字典目录
       List<SystemDictionary> systemDictionarys = systemDictionaryService.getAll();
       model.addAttribute("pageInfo", pageInfo);
       model.addAttribute("systemDictionarys", systemDictionarys);
        return "systemDictionaryItem/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
            if (id != null) {
                systemDictionaryItemService.delete(id);
            }
            return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
       //如果编辑/添加明细中sequence没填,先找出同一parentId下最大sequence,然后加1在设置回去
           if(systemDictionaryItem.getSequence()==null){
               int maxSequence=systemDictionaryItemService.getMaxSequence(systemDictionaryItem.getParentId());
               systemDictionaryItem.setSequence(maxSequence);
           }
            if (systemDictionaryItem.getId() != null) {
                systemDictionaryItemService.update(systemDictionaryItem);
            } else {
                systemDictionaryItemService.save(systemDictionaryItem);
            }
            return new JsonResult();
    }

}
